
#include "mbm_spreadtrum_inc.h"
#include "mbm_typedefine.h"
#include "mbm_camera_adapter.h"

#ifdef MBM_DEBUG_CAMERA
#define MBM_CAMERA_TRACE mbm_trace
#else
#define MBM_CAMERA_TRACE
#endif

typedef struct
{
    UInt32 y_addr;
    UInt32 uv_addr;
    UInt32 width;
    UInt32 height;

}t_mbm_scale_param;
/*
extern ISP_SERVICE_T        s_isp_service_info;

int mbm_camera_get_data(ISP_DATA_ADDR_T addr,UInt32 width,UInt32 height)
{
	ISP_SERVICE_T *s=&s_isp_service_info;
    s->service=ISP_SERVICE_CAPTURE;         
    _ISP_ServiceSendMsg(s->service,ISP_SERVICE_START_SIG,0,0,0);
}
*/
static void mbm_camera_capture_init(ISP_JPEG_PARAM_T* isp_param, t_mbm_camera_capture_param* param)
{  
    DCAM_CONTEXT_T * pContext = _GetDCAMContext();
    SENSOR_EXP_INFO_T*      sensor_info_ptr = pContext->sensor_info_ptr;
    DCAMERA_RECT_T          src_rect={0};

    MBM_CAMERA_TRACE("mbm_camera_capture_init buf0x%x len0x%x", param->capture_buf, param->capture_len);
      
    isp_param->input_size.w = sensor_info_ptr->sensor_mode_info[SENSOR_MODE_COMMON_INIT].width;
    isp_param->input_size.h = sensor_info_ptr->sensor_mode_info[SENSOR_MODE_COMMON_INIT].height;

    //config isp input to sensor rect
    isp_param->input_rect.x = 0x00;
    isp_param->input_rect.y = 0x00;
    isp_param->input_rect.w = isp_param->input_size.w;
    isp_param->input_rect.h = isp_param->input_size.h;

    //configure isp output size
    isp_param->output_size.w = param->capture_width;
    isp_param->output_size.h = param->capture_height;

    isp_param->skip_num=0x00;

    isp_param->capture_addr= param->capture_buf;
    isp_param->capture_buf_size = param->capture_len;

    //isp_param->get_cap_data = mbm_camera_get_data;//aync
    isp_param->get_cap_data = 0;//sync

    isp_param->sensor_data_format=ISP_DATA_YUV422;    
    isp_param->output_data_format=ISP_DATA_YUV422;

    //config to capture rect
    src_rect.x = 0x00;
    src_rect.y = 0x00;
    src_rect.w = param->capture_width;
    src_rect.h = param->capture_height;

    //adjust
    DC_AdjRect_OnDistortion(&isp_param->input_rect, &src_rect, DCAMERA_DIST_MODE_TRIMMING); 
}

UInt32 mbm_camera_capture(t_yuv_data* data, t_mbm_camera_capture_param* param)
{
    ISP_DATA_ADDR_T pispdata;
    
    ISP_JPEG_PARAM_T isp_capture={0x00};
    
    uint16 capture_width;
    uint16 capture_height;

    mbm_camera_capture_init(&isp_capture, param);

    ISP_ServiceOpen();
	
	//ISP_ServiceSetCapDeci(ISP_SERVICE_CAP_DECI_DISABLE);	//for w10
    if(SCI_SUCCESS == ISP_ServiceSetCaptureParam((&isp_capture)))
    {
        if(SCI_SUCCESS != ISP_ServiceStartSyncCapture(&pispdata, &capture_width, &capture_height))
        {
            mbm_trace("ISP_ServiceStartSyncCapture error");
            ISP_ServiceClose();
            return SCI_ERROR;
        }
    }
    else
    {
        mbm_trace("ISP_ServiceSetCaptureParam error");
        ISP_ServiceClose();
        return SCI_ERROR;
    }
//	mbm_trace("cap:%d-%d\n", capture_width, capture_height);
    
    ISP_ServiceClose();

    if (data)
    {
        data->uv_addr = pispdata.uv_addr;
        data->y_addr = pispdata.y_addr;
    }
    
    return SCI_TRUE;
}

int mbm_camera_open(void)
{
    _DCAMERA_InitSensor();
}

int mbm_camera_close(void)
{
    Sensor_Close();
}

